package cz.vargagab.domain;

import java.io.Serializable;

/**
 * Class representing the access request to a shared resource
 * @author Gabriel Varga
 *
 */
public class Request implements Comparable<Request>,Serializable {

	private static final long serialVersionUID = -440782241644716332L;
	
	private long timestamp;
	private ClientRmiNode requestingNode;
	private OperationType operationType;
	
	public Request() {
	}

	public Request(long timestamp, ClientRmiNode requestingNode,
			OperationType operationType) {
		super();
		this.timestamp = timestamp;
		this.requestingNode = requestingNode;
		this.operationType = operationType;
	}

	public long getTimestamp() {
		return timestamp;
	}
	public void setTimestamp(long timestamp) {
		this.timestamp = timestamp;
	}

	public ClientRmiNode getRequestingNode() {
		return requestingNode;
	}
	public void setRequestingNode(ClientRmiNode requestingNode) {
		this.requestingNode = requestingNode;
	}

	public OperationType getOperationType() {
		return operationType;
	}
	public void setOperationType(OperationType operationType) {
		this.operationType = operationType;
	}

	// radenie requestov podla timestamp
	public int compareTo(Request o) {
		final int SMALLER = -1;
		final int EQUAL = 0;
		final int LARGER = 1;
		  
		Request otherRequest = (Request) o;
		if (this.timestamp == otherRequest.timestamp) return EQUAL;
		else if (this.timestamp < otherRequest.timestamp) return SMALLER;
		else return LARGER;
		
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((operationType == null) ? 0 : operationType.hashCode());
		result = prime * result
				+ ((requestingNode == null) ? 0 : requestingNode.hashCode());
		result = prime * result + (int) (timestamp ^ (timestamp >>> 32));
		return result;
	}

	// rovnaju sa requesty?
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Request other = (Request) obj;
		if (operationType != other.operationType)
			return false;
		if (requestingNode == null) {
			if (other.requestingNode != null)
				return false;
		} else if (!requestingNode.equals(other.requestingNode))
			return false;
		if (timestamp != other.timestamp)
			return false;
		return true;
	}
	
}
